package com.lingnan.tijian.service.impl;

import com.lingnan.tijian.domain.Orders;
import com.lingnan.tijian.dto.ApiResponse;
import com.lingnan.tijian.dto.OrderDateAndHpnameDto;
import com.lingnan.tijian.exception.TijianAPPExceptionEnum;
import com.lingnan.tijian.exception.TijianAppException;
import com.lingnan.tijian.mapper.OrdersMapper;
import com.lingnan.tijian.service.ifac.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    OrdersMapper ordersMapper;


    @Override
    public ApiResponse listOrdersByUserIdAndState(Orders orders) {
        return new ApiResponse(ApiResponse.SUCCESS_STATUS,ApiResponse.SUCCESS_DESC,ordersMapper.selectByUserIdAndState(orders));
    }

    @Transactional
    @Override
    public ApiResponse saveOrders(Orders orders) {
        orders.setState(1);//新创建的订单，状态默认是1
        //③先查一下该用户  当天   有没有已经预约(未归档)的订单，没有的话再保存订单数据
        List<OrderDateAndHpnameDto> orderDateAndHpnameDtoList=ordersMapper.selectByUserIdAndStateAndOrderDate(orders);
        if(orderDateAndHpnameDtoList!=null||orderDateAndHpnameDtoList.size()!=0){
            throw new TijianAppException(TijianAPPExceptionEnum.ALREADY_APPOINTMENT_CURRENTDAY.getCode(),TijianAPPExceptionEnum.ALREADY_APPOINTMENT_CURRENTDAY.getMessage());
        }
        //②先查一下有没有余号，再保存订单数据  余号=总号数-已经预约的号数
        // 获取所选医院每天的约号最大数量
       // String[] ruleArr=hospitalMapper.selectByPrimaryKey(hpId).getRule().split(",");
        //如何获取已经预约的号数：  select count(*) from orders where hpId=#{hpId} and state=1 and orderDate=#{orderDate}
        //OrdersMapper中定义对应上述sql的方法
        /*
        @Select("select count(*) from orders where hpId=#{hpId} and state=1 and orderDate=#{orderDate}")
        public int getExistingNumber(Orders orders);

         */
//        if(remainder==0){
//            throw new TijianAppException(TijianAPPExceptionEnum.ALREADY_APPOINTMENT_CURRENTDAY.setCode(),TijianAPPExceptionEnum.ALREADY_APPOINTMENT_CURRENTDAY.getMessage());
      //  }
        ordersMapper.insertSelective(orders);  //订单Id的创建：UUID   雪花算法
       return new ApiResponse(ApiResponse.SUCCESS_STATUS,ApiResponse.SUCCESS_DESC);
    }

//    public static void main(String[] args) {
//       // System.out.println(UUID.randomUUID().toString().replace("-",""));
//    }
}









